package cn.changgou.controller;

import cn.changgou.entity.Result;
import cn.changgou.entity.StatusCode;
import cn.changgou.goods.pojo.Album;
import cn.changgou.service.AlbumService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/album")
@CrossOrigin
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /*相册分页条件查询*/
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result<PageInfo> findAlbumPage(@PathVariable Integer page,
                                     @PathVariable Integer size,
                                     @RequestBody Album album){
        PageInfo<Album> list = albumService.findAlbumPage(album, page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "相册分页条件查询成功！", list);
    }

    /*相册分页查询*/
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
    public Result<PageInfo> albumByPage(@PathVariable Integer page,
                                     @PathVariable Integer size){
        PageInfo<Album> list = albumService.albumByPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "相册分页查询成功！", list);
    }

    /*根据条件查询相册列表*/
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Result<Album> findLit(@RequestBody Album album){
        List<Album> list = albumService.findList(album);
        return new Result<Album>(true, StatusCode.OK, "根据条件查询相册列表成功！", list);
    }

    /*根据ID删除相册*/
    @RequestMapping(value = "/{albumId}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable Long albumId){
        albumService.delete(albumId);
        return new Result(true, StatusCode.OK, "根据ID删除相册成功！");
    }

    /*根据ID修改相册*/
    @RequestMapping(value = "/{albumId}", method = RequestMethod.PUT)
    public Result update(@PathVariable Long albumId, @RequestBody Album album){
        album.setId(albumId);
        albumService.update(album);
        return new Result(true, StatusCode.OK, "根据ID修改相册成功！");
    }

    /*根据ID查询相册*/
    @RequestMapping(value = "/{albumId}", method = RequestMethod.GET)
    public Result<Album> findOne(@PathVariable Long albumId){
        Album album = albumService.findOne(albumId);
        return new Result<Album>(true, StatusCode.OK, "根据ID查询相册成功！", album);
    }

    /*增加相册*/
    @RequestMapping(method = RequestMethod.POST)
    public Result addAlbum(@RequestBody Album album){
        albumService.add(album);
        return new Result(true, StatusCode.OK, "新增相册成功！");
    }

    /*查询所有相册*/
    @RequestMapping(method = RequestMethod.GET)
    public Result<List<Album>> findAll(){
        List<Album> albumList = albumService.findAll();
        return new Result<List<Album>>(true, StatusCode.OK, "查询所有相册成功！", albumList);
    }
}
